<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\SystemHelp\Action\Dashboard;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Contract\RbacMenuContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\SystemHelp\Contract\SystemHelpContract;

/**
 * 详情.
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    protected $logger;

    /**
     * @Inject
     * @var SystemHelpContract
     */
    protected $systemHelpService;

    /**
     * @Inject
     * @var RbacMenuContract
     */
    protected $rbacMenuService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/systemHelp/index", methods="get")
     */
    public function handle(): array
    {
        $list = $this->systemHelpService->getMenuDefaultSystemHelps(['id', 'name', 'url', 'menu_id']);
        if (empty($list)) {
            return [];
        }

        foreach ($list as &$item) {
            $menu = $this->rbacMenuService->getRbacMenuById($item['menuId']);
            $item['path'] = $menu['linkUrl'] ?? '';
        }
        unset($item);
        return $list;
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
        ];
    }
}
